package com.xls.recycleranimation;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/19.
 */

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHoler> {

    private List<MessageInfo> list = new ArrayList<>();

    @Override
    public ViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =  LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message,parent,false);
        return new ViewHoler(view);
    }

    @Override
    public void onBindViewHolder(ViewHoler holder, int position) {
        MessageInfo messageInfo =  list.get(position);
        holder.nameText.setText(messageInfo.getName());
        holder.timeText.setText(String.valueOf(messageInfo.getTime()));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHoler extends RecyclerView.ViewHolder{
        private TextView nameText;
        private TextView timeText;
        public ViewHoler(View itemView) {
            super(itemView);
            nameText = (TextView) itemView.findViewById(R.id.tv_name);
            timeText = (TextView) itemView.findViewById(R.id.tv_time);
        }
    }

    public void setList(List<MessageInfo> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public void addItem(MessageInfo messageInfo){
        this.list.add(0,messageInfo);
        notifyItemInserted(0);
    }

    public void removeItem(){
        if(this.list.size()>0){
            this.list.remove(0);
            notifyItemRemoved(0);
        }
    }
}
